using System;
using UnityEngine;

[ExecuteInEditMode]
public class ControllerRoller : ControllerGeneric
{
	private float unit = 1f;

	public float length = 10f;

	public float width = 3f;

	public float height = 3f;

	public GameObject roller;

	public GameObject centerFront;

	public GameObject nutFront;

	public GameObject centerBack;

	public GameObject nutBack;

	public bool includeNuts;

	public bool isElliptical;

	private float nutOffset;

	private float nutLength = 0.1f;

	public float maxAngle = 15f;

	public bool yoyoLoop;

	public float rotationSpeed = 10f;

	public float startDelay;

	public Vector2[] uvsOriginal;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		Roller roller = this.roller.AddComponent<Roller>();
		roller.angleLimit = maxAngle;
		roller.yoyoLoop = yoyoLoop;
		roller.rotationSpeed = rotationSpeed;
		roller.startDelay = startDelay;
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			UnityEngine.Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		if (needsRefresh)
		{
			uvsOriginal = null;
			needsRefresh = false;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			length = remoteControl.transform.localScale.z;
			width = remoteControl.transform.localScale.x;
			height = remoteControl.transform.localScale.y;
			if (!isElliptical)
			{
				height = width;
			}
		}
		if (length < 0.5f)
		{
			length = 0.5f;
		}
		if (width < 0.5f)
		{
			width = 0.5f;
		}
		if (height < 0.5f)
		{
			height = 0.5f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.transform.localScale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
		roller.transform.localPosition = Vector3.zero;
		roller.transform.localEulerAngles = new Vector3(-90f, 0f, 0f);
		if (includeNuts)
		{
			roller.transform.localScale = new Vector3(width / unit, (length - 2f * (nutLength - nutOffset)) / unit, height / unit);
			nutFront.transform.localPosition = -Vector3.forward * (length * 0.5f - nutLength);
			nutBack.transform.localPosition = Vector3.forward * (length * 0.5f - nutLength);
			centerFront.transform.localPosition = -Vector3.forward * (length * 0.5f - nutLength);
			centerBack.transform.localPosition = Vector3.forward * (length * 0.5f - nutLength);
		}
		else
		{
			roller.transform.localScale = new Vector3(width / unit, length / unit, height / unit);
			nutFront.transform.localPosition = -Vector3.forward * (length * 0.5f - nutOffset);
			nutBack.transform.localPosition = Vector3.forward * (length * 0.5f - nutOffset);
			centerFront.transform.localPosition = -Vector3.forward * (length * 0.5f);
			centerBack.transform.localPosition = Vector3.forward * (length * 0.5f);
		}
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			remoteControl.gameObject.AddComponent<PresetsRoller>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		PresetsRoller component = rc.gameObject.GetComponent<PresetsRoller>();
		PresetsRoller component2 = remoteControl.gameObject.GetComponent<PresetsRoller>();
		component2.includeNuts = component.includeNuts;
		component2.maxAngle = component.maxAngle;
		component2.yoyoLoop = component.yoyoLoop;
		component2.rotationSpeed = component.rotationSpeed;
		component2.startDelay = component.startDelay;
		component2.isElliptical = component.isElliptical;
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
		centerFront.transform.SetParent(roller.transform);
		centerBack.transform.SetParent(roller.transform);
	}

	private void SetNames()
	{
		roller.name = "Roller";
		centerFront.name = "Roller Center Front";
		centerBack.name = "Roller Center Back";
		nutFront.name = "Roller Nut Front";
		nutBack.name = "Roller Nut Back";
	}

	public void SetupUVs()
	{
		Mesh mesh = roller.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] vertices = mesh.vertices;
		if (uvsOriginal == null)
		{
			uvsOriginal = mesh.uv;
		}
		if (uvsOriginal.Length == 0)
		{
			uvsOriginal = mesh.uv;
		}
		float num = (float)Math.PI * (width + height) / 2f;
		float num2 = (float)Math.PI * 4f;
		float num3 = num2 / num / 4f;
		int[] triangles = mesh.GetTriangles(0);
		float num4 = length;
		if (includeNuts)
		{
			num4 = length - 2f * (nutLength - nutOffset);
		}
		foreach (int num5 in triangles)
		{
			Vector3 vector = vertices[num5];
			if (vector.y > 0f)
			{
				uv[num5].y = 0.5f - num4 * num3 / 2f;
			}
			else
			{
				uv[num5].y = 0.5f + num4 * num3 / 2f;
			}
		}
		mesh.uv = uv;
	}
}
